function Messenger(user){
	if(!user) user = global.getObject('me');

	this.messageTextElement = global.getElement('messageText');
	this.room = global.getObject('room').id;
	this.user = user;
	this.com = new Communicator();
	this.com.addParameter('type','user');
	this.com.addParameter('task','sendMessage');

	this.replyTo = null;
	this.counter = 0;
	this.messages = new Array();
}
function encodeURL(str){
	str = str.split('\n').join('\\n');
    return encodeURIComponent(str).replace(/[!'()]/g, escape).replace(/\*/g, "%2A");
}

Messenger.prototype.send = function(){
	var text = this.messageTextElement.value;
	this.counter++;

	if(this.validate(text)){
		this.com.addParameter('data',JSON.stringify({room: this.room, user: this.user.id, text: encodeURL(text), parent: this.replyTo, counter: this.counter}));
		this.com.sendRequest(sendMessageHandler);
		
		var time = getDate();
		var message = new Message();
		message.assignProperties(this.user,text,time,this.replyTo);
		message.place();

		this.messageTextElement.value = '';
		this.messageTextElement.focus();
		var height = getData(this.messageTextElement,'height');
		this.messageTextElement.style.height = height+'px';

		this.messages['m_'+this.counter] = message;

		if(this.replyTo != null) this.stopReplying();
	}
}

Messenger.prototype.validate = function(text){
	var temp;
	
	//check if it has only line breaks
	temp = text.replace(/\n/g,'');
	if(temp == '') return false;
	//check if it has only white spaces
	temp = temp.replace(/ /g,'');
	if(temp == '') return false;

	return true;
}

Messenger.prototype.updateMessage = function(counter, id, parentRepliesNumber){
	var key = 'm_'+counter;
	var message = this.messages[key];

	message.setId(id);
	if(isNumeric(parentRepliesNumber)){
		message.setParentRepliesNumber(parentRepliesNumber);
		message.updateParentElement();
	} 
	
	delete this.messages[key];
}

Messenger.prototype.reply = function(messageId){
	this.replyTo = messageId;
}

Messenger.prototype.stopReplying = function(){
	var reply = global.getElement('reply');

	this.replyTo = null;

	addClass(reply, 'invisible');
}

function replyMessage(event){
	var caller = event.target;
	var message = findParentWithClass(caller, 'message');

	var messageId = getData(message,'id');
	var userId = getData(message,'user');
	var text = message.getElementsByClassName('text')[0].innerHTML;
	var user = global.getObject('user', 'user_'+userId);
	
	var me = global.getObject('me');

	var reply = global.getElement('reply');
	//var replyTo = global.getElement('replyTo');
	var replyMessage = global.getElement('replyMessage');

	me.messenger.reply(messageId);

	if(text.length > 40) text = text.substr(0,40)+'...';

	removeClass(reply, 'invisible');
	//replyTo.innerHTML = user.name;
	replyMessage.innerHTML = text;

	global.getElement('messageText').focus();
}

function stopReplying(){
	var me = global.getObject('me');

//	var replyTo = global.getElement('replyTo');
	//var replyMessage = global.getElement('replyMessage');

	me.messenger.stopReplying();
}

function sendMessageHandler(response){ //alert(response)
	var obj = eval(response);
	//if(obj.length == 2) obj[2] = null;
	var me = global.getObject('me');
	me.messenger.updateMessage(obj[0],obj[1][0],obj[1][1]);
}

function bindText(){
	var ta = event.target;

	var maxHeight = getData(ta,'maxHeight');
	var height = getData(ta,'height');

	var scrollHeight = ta.scrollHeight;
	var currentHeight = ta.offsetHeight;
 	
	if(scrollHeight > currentHeight && currentHeight < maxHeight){
		ta.style.height = ta.scrollHeight+'px';
	}
}